package map;

import Tree.BST;

public class BSTMap<K extends Comparable<K>,V> implements Map<K,V>{

    private class Node{
        K key;
        V value;
        Node right;
        Node left;

        public Node(){
            this(null,null,null,null);
        }

        public Node(K key,V value){
            this(key,value,null,null);
        }

        public Node(K key,V value,Node left,Node right){
            this.key = key;
            this.value = value;
            this.left = left;
            this.right = right;
        }

        @Override
        public String toString() {
            return "key:"+key+",value:"+value;
        }
    }
    private Node root;
    private int size;

    public BSTMap(){
        root = new Node();
        size = 0;
    }

    public BSTMap(K key,V value){
        root = new Node(key,value);
        size = 1;
    }

    @Override
    public void add(K key, V value) {
        if (root == null) {
            root = new Node(key,value);
            return;
        }
        addNode(root,key,value);
    }

    private Node addNode(Node node,K key, V value){
        if (node == null) {
            size++;
            return new Node(key,value);
        }
        if (key.compareTo(node.key) < 0) {
            node.left = addNode(node.left,key,value);
        }else if(key.compareTo(node.key) > 0){
            node.right = addNode(node.right,key,value);
        }
        return node;
    }

    @Override
    public V remove(K key) {
        return null;
    }

    @Override
    public boolean contains(K key) {
        return getNode(root,key) == null ? false:true;
    }

    @Override
    public V get(K key) {
        if (root == null)
            throw new RuntimeException("不存在根元素");
        return getNode(root,key).value;
    }

    private Node getNode(Node node,K key){
        if (node == null) {
           return null;
        }
        if (key.compareTo(node.key) < 0) {
            return getNode(node.left,key);
        }else if(key.compareTo(node.key) > 0){
            return getNode(node.right,key);
        }else
            return node;
    }

    @Override
    public void set(K key, V value) {
        Node node = getNode(root, key);
        if (node != null) {
            if (!value.equals(node.value)) {
                node.value = value;
            }
        }
        throw new RuntimeException("元素不存在");

    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }



    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        generateBSTString(root,0,sb);
        return  sb.toString();
    }

    private void generateBSTString(Node node, int depth, StringBuilder sb){
        if (node == null) {
            sb.append(generateDepthString(depth)+"null\n");
            return;
        }
        sb.append(generateDepthString(depth)+"key:"+node.key+" ,value"+node.value+"\n");
        generateBSTString(node.left,depth+1,sb);
        generateBSTString(node.right,depth+1,sb);
    }

    private String generateDepthString(int depth){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append("---");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        BSTMap bstMap = new BSTMap(5,"根节点");
        for (int i = 0; i < 10; i++) {
            bstMap.add(i,"第"+i);
        }
        System.out.println(bstMap);
    }
}
